package ciyuan.Block.Machine.Simple;

import ciyuan.Block.Machine.Simple.Anvil.Anvil;
import ciyuan.Block.Machine.Simple.Anvil.AnvilContainer;
import ciyuan.Block.Machine.Simple.Anvil.AnvilScreen;
import ciyuan.Block.Machine.Simple.Anvil.AnvilTileEntity;
import ciyuan.Block.Machine.Simple.Burnisher.Burnisher;
import ciyuan.Block.Machine.Simple.Burnisher.BurnisherContainer;
import ciyuan.Block.Machine.Simple.Burnisher.BurnisherScreen;
import ciyuan.Block.Machine.Simple.Burnisher.BurnisherTileEntity;
import ciyuan.Block.Machine.Simple.CastTable.CastTable;
import ciyuan.Block.Machine.Simple.CastTable.CastTableContainer;
import ciyuan.Block.Machine.Simple.CastTable.CastTableScreen;
import ciyuan.Block.Machine.Simple.CastTable.CastTableTileEntity;
import ciyuan.Block.Machine.Simple.ForgingFurnace.ForgingFurnace;
import ciyuan.Block.Machine.Simple.ForgingFurnace.ForgingFurnaceContainer;
import ciyuan.Block.Machine.Simple.ForgingFurnace.ForgingFurnaceScreen;
import ciyuan.Block.Machine.Simple.ForgingFurnace.ForgingFurnaceTileEntity;
import ciyuan.Block.Machine.Simple.Furnace.SimpleFurnace;
import ciyuan.Block.Machine.Simple.Furnace.SimpleFurnaceContainer;
import ciyuan.Block.Machine.Simple.Furnace.SimpleFurnaceScreen;
import ciyuan.Block.Machine.Simple.Furnace.SimpleFurnaceTileEntity;
import ciyuan.Block.Machine.Simple.Grinder.Grinder;
import ciyuan.Block.Machine.Simple.Grinder.GrinderContainer;
import ciyuan.Block.Machine.Simple.Grinder.GrinderScreen;
import ciyuan.Block.Machine.Simple.Grinder.GrinderTileEntity;
import ciyuan.Block.Machine.Simple.Smelter.Smelter;
import ciyuan.Block.Machine.Simple.Smelter.SmelterContainer;
import ciyuan.Block.Machine.Simple.Smelter.SmelterScreen;
import ciyuan.Block.Machine.Simple.Smelter.SmelterTileEntity;
import ciyuan.Block.Machine.Simple.ToolMakingTable.ToolMakingTable;
import ciyuan.Block.Machine.Simple.ToolMakingTable.ToolMakingTableContainer;
import ciyuan.Block.Machine.Simple.ToolMakingTable.ToolMakingTableScreen;
import ciyuan.Block.Machine.Simple.ToolMakingTable.ToolMakingTableTileEntity;
import ciyuan.MainClass;
import net.minecraft.block.Block;
import net.minecraft.client.gui.ScreenManager;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.ContainerType;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntityType;
import net.minecraftforge.common.extensions.IForgeContainerType;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLConstructModEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;

import static ciyuan.Item.ItemGroups.Machine;

/**
 * @author 150149
 */
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
public class SimpleMachineRegister {

    public static final DeferredRegister<TileEntityType<?>> MACHINE_TILE_ENTITIES = DeferredRegister.create(ForgeRegistries.TILE_ENTITIES, MainClass.MODID);
    public static final DeferredRegister<Block> MACHINE_BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MainClass.MODID);
    public static final DeferredRegister<Item> MACHINE_ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MainClass.MODID);
    public static final DeferredRegister<ContainerType<?>> CONTAINERS = DeferredRegister.create(ForgeRegistries.CONTAINERS, MainClass.MODID);

    public static final RegistryObject<Block> SIMPLE_FURNACE_BLOCK = MACHINE_BLOCKS.register("simple_furnace", SimpleFurnace::new);
    public static final RegistryObject<Block> CAST_TABLE_BLOCK = MACHINE_BLOCKS.register("cast_table", CastTable::new);
    public static final RegistryObject<Block> SMELTER_BLOCK = MACHINE_BLOCKS.register("smelter", Smelter::new);
    public static final RegistryObject<Block> GRINDER_BLOCK = MACHINE_BLOCKS.register("grinder", Grinder::new);
    public static final RegistryObject<Block> BURNISHER_BLOCK = MACHINE_BLOCKS.register("burnisher", Burnisher::new);
    public static final RegistryObject<Block> ANVIL_BLOCK = MACHINE_BLOCKS.register("anvil", Anvil::new);
    public static final RegistryObject<Block> TOOL_MAKING_TABLE_BLOCK = MACHINE_BLOCKS.register("tool_making_table", ToolMakingTable::new);
    public static final RegistryObject<Block> FORGING_FURNACE_BLOCK = MACHINE_BLOCKS.register("forging_furnace", ForgingFurnace::new);

    public static final RegistryObject<Item> SIMPLE_FURNACE_ITEM = MACHINE_ITEMS.register("simple_furnace", () -> new BlockItem(SIMPLE_FURNACE_BLOCK.get(), new Item.Properties().tab(Machine)));
    public static final RegistryObject<Item> CAST_TABLE_ITEM = MACHINE_ITEMS.register("cast_table", () -> new BlockItem(CAST_TABLE_BLOCK.get(), new Item.Properties().tab(Machine)));
    public static final RegistryObject<Item> SMELTER_ITEM = MACHINE_ITEMS.register("smelter", () -> new BlockItem(SMELTER_BLOCK.get(), new Item.Properties().tab(Machine)));
    public static final RegistryObject<Item> GRINDER_ITEM = MACHINE_ITEMS.register("grinder", () -> new BlockItem(GRINDER_BLOCK.get(), new Item.Properties().tab(Machine)));
    public static final RegistryObject<Item> BURNISHER_ITEM = MACHINE_ITEMS.register("burnisher", () -> new BlockItem(BURNISHER_BLOCK.get(), new Item.Properties().tab(Machine)));
    public static final RegistryObject<Item> ANVIL_ITEM = MACHINE_ITEMS.register("anvil", () -> new BlockItem(ANVIL_BLOCK.get(), new Item.Properties().tab(Machine)));
    public static final RegistryObject<Item> TOOL_MAKING_TABLE_ITEM = MACHINE_ITEMS.register("tool_making_table", () -> new BlockItem(TOOL_MAKING_TABLE_BLOCK.get(), new Item.Properties().tab(Machine)));
    public static final RegistryObject<Item> FORGING_FURNACE_ITEM = MACHINE_ITEMS.register("forging_furnace", () -> new BlockItem(FORGING_FURNACE_BLOCK.get(), new Item.Properties().tab(Machine)));

    public static final RegistryObject<TileEntityType<SimpleFurnaceTileEntity>> SIMPLE_FURNACE_TILE_ENTITY = MACHINE_TILE_ENTITIES.register("simple_furnace", () -> TileEntityType.Builder.of(SimpleFurnaceTileEntity::new, SIMPLE_FURNACE_BLOCK.get()).build(null));
    public static final RegistryObject<TileEntityType<CastTableTileEntity>> CAST_TABLE_TILE_ENTITY = MACHINE_TILE_ENTITIES.register("cast_table", () -> TileEntityType.Builder.of(CastTableTileEntity::new, CAST_TABLE_BLOCK.get()).build(null));
    public static final RegistryObject<TileEntityType<SmelterTileEntity>> SMELTER_TILE_ENTITY = MACHINE_TILE_ENTITIES.register("smelter", () -> TileEntityType.Builder.of(SmelterTileEntity::new, SMELTER_BLOCK.get()).build(null));
    public static final RegistryObject<TileEntityType<GrinderTileEntity>> GRINDER_ENTITY = MACHINE_TILE_ENTITIES.register("grinder", () -> TileEntityType.Builder.of(GrinderTileEntity::new, GRINDER_BLOCK.get()).build(null));
    public static final RegistryObject<TileEntityType<BurnisherTileEntity>> BURNISHER_ENTITY = MACHINE_TILE_ENTITIES.register("burnisher", () -> TileEntityType.Builder.of(BurnisherTileEntity::new, BURNISHER_BLOCK.get()).build(null));
    public static final RegistryObject<TileEntityType<AnvilTileEntity>> ANVIL_ENTITY = MACHINE_TILE_ENTITIES.register("anvil", () -> TileEntityType.Builder.of(AnvilTileEntity::new, ANVIL_BLOCK.get()).build(null));
    public static final RegistryObject<TileEntityType<ToolMakingTableTileEntity>> TOOL_MAKING_TABLE_ENTITY = MACHINE_TILE_ENTITIES.register("tool_making_table", () -> TileEntityType.Builder.of(ToolMakingTableTileEntity::new, TOOL_MAKING_TABLE_BLOCK.get()).build(null));
    public static final RegistryObject<TileEntityType<ForgingFurnaceTileEntity>> FORGING_FURNACE_ENTITY = MACHINE_TILE_ENTITIES.register("forging_furnace", () -> TileEntityType.Builder.of(ForgingFurnaceTileEntity::new, FORGING_FURNACE_BLOCK.get()).build(null));

    public static final RegistryObject<ContainerType<SimpleFurnaceContainer>> SIMPLE_FURNACE_CONTAINER = CONTAINERS.register("simple_furnace", () -> IForgeContainerType.create((int windowId, PlayerInventory inv, PacketBuffer data) -> new SimpleFurnaceContainer(ContainerType.GENERIC_3x3, windowId, inv, data.readBlockPos())));
    public static final RegistryObject<ContainerType<CastTableContainer>> CAST_TABLE_CONTAINER = CONTAINERS.register("cast_table", () -> IForgeContainerType.create((int windowId, PlayerInventory inv, PacketBuffer data) -> new CastTableContainer(ContainerType.GENERIC_3x3, windowId, inv, data.readBlockPos())));
    public static final RegistryObject<ContainerType<SmelterContainer>> SMELTER_CONTAINER = CONTAINERS.register("smelter", () -> IForgeContainerType.create((int windowId, PlayerInventory inv, PacketBuffer data) -> new SmelterContainer(ContainerType.GENERIC_3x3, windowId, inv, data.readBlockPos())));
    public static final RegistryObject<ContainerType<GrinderContainer>> GRINDER_CONTAINER = CONTAINERS.register("grinder", () -> IForgeContainerType.create((int windowId, PlayerInventory inv, PacketBuffer data) -> new GrinderContainer(ContainerType.GENERIC_3x3, windowId, inv, data.readBlockPos())));
    public static final RegistryObject<ContainerType<BurnisherContainer>> BURNISHER_CONTAINER = CONTAINERS.register("burnisher", () -> IForgeContainerType.create((int windowId, PlayerInventory inv, PacketBuffer data) -> new BurnisherContainer(ContainerType.GENERIC_3x3, windowId, inv, data.readBlockPos())));
    public static final RegistryObject<ContainerType<AnvilContainer>> ANVIL_CONTAINER = CONTAINERS.register("anvil", () -> IForgeContainerType.create((int windowId, PlayerInventory inv, PacketBuffer data) -> new AnvilContainer(ContainerType.GENERIC_3x3, windowId, inv, data.readBlockPos())));
    public static final RegistryObject<ContainerType<ToolMakingTableContainer>> TOOL_MAKING_TABLE_CONTAINER = CONTAINERS.register("tool_making_table", () -> IForgeContainerType.create((int windowId, PlayerInventory inv, PacketBuffer data) -> new ToolMakingTableContainer(ContainerType.GENERIC_3x3, windowId, inv, data.readBlockPos())));
    public static final RegistryObject<ContainerType<ForgingFurnaceContainer>> FORGING_FURNACE_CONTAINER = CONTAINERS.register("forging_furnace", () -> IForgeContainerType.create((int windowId, PlayerInventory inv, PacketBuffer data) -> new ForgingFurnaceContainer(ContainerType.GENERIC_3x3, windowId, inv, data.readBlockPos())));

    @SubscribeEvent
    public static void onClientSetupEvent(FMLClientSetupEvent event) {
        event.enqueueWork(() -> {
            ScreenManager.register(SIMPLE_FURNACE_CONTAINER.get(), SimpleFurnaceScreen::new);
            ScreenManager.register(CAST_TABLE_CONTAINER.get(), CastTableScreen::new);
            ScreenManager.register(SMELTER_CONTAINER.get(), SmelterScreen::new);
            ScreenManager.register(GRINDER_CONTAINER.get(), GrinderScreen::new);
            ScreenManager.register(BURNISHER_CONTAINER.get(), BurnisherScreen::new);
            ScreenManager.register(ANVIL_CONTAINER.get(), AnvilScreen::new);
            ScreenManager.register(TOOL_MAKING_TABLE_CONTAINER.get(), ToolMakingTableScreen::new);
            ScreenManager.register(FORGING_FURNACE_CONTAINER.get(), ForgingFurnaceScreen::new);
        });
    }

    @SubscribeEvent
    public static void onFMLConstructModEvent(FMLConstructModEvent event) {
        MACHINE_BLOCKS.register(FMLJavaModLoadingContext.get().getModEventBus());
        MACHINE_ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus());
        MACHINE_TILE_ENTITIES.register(FMLJavaModLoadingContext.get().getModEventBus());
        CONTAINERS.register(FMLJavaModLoadingContext.get().getModEventBus());
    }
}
